(function () {
    "use strict";

    var app = angular.module('app');

    app.controller('wct.reply', WctReplyCtrl);

    WctReplyCtrl.$inject = [
        '$scope',
        'A',
        '$toast',
        'Api',
        'WctReplyService'
    ];

    function WctReplyCtrl($scope, A, $toast, Api, WctReplyService) {
        $scope.progress = A.PromiseTracker();
        //$scope.wxButtonTree = {};

        $scope.options = {
            api       : Api.wct.menu.get,
            dataIndex : 'buttons',
            childIndex: 'subButtons',
            isLeaf    : function (row) {
                return row.subButtons.length === 0;
            },
            column    : [{
                text : '按钮名',
                name : 'name',
                ratio: 3,
                tree : true
            }, {
                text: '类型',
                name: 'type'
            }, {
                text : '链接',
                name : 'url',
                ratio: 5
            }, {
                name : '操作列',
                items: [{
                    icon   : 'fa fa-cog',
                    tooltip: '设置',
                    handler: function (o, row) {
                        console.log(o, row);
                    }
                }, {
                    icon   : 'fa fa-remove',
                    tooltip: '删除',
                    handler: function (o, row) {
                        console.log(o, row);
                    }
                }]
            }]
        };

        $scope.sync = function () {
            var promise = WctReplyService.sync().then(function (result) {
                if (result.success) {
                    $toast.success("同步成功!");
                    $scope.wxButtonTree.reload();
                } else {
                    $toast.error(result.msg || "同步失败!");
                }
            });
            $scope.progress.addPromise(promise);
        }
    }
})();